//
//  JCClubPasswordInputView.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/12/5.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCClubPasswordInputView.h"

@interface JCClubPasswordInputView ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UITextField *input;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (strong, nonatomic) NSString *password;

@end

@implementation JCClubPasswordInputView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    [self initCode];
}

- (void)initCode{
    _btn.layer.cornerRadius = 8;
    _btn.layer.masksToBounds = YES;
    [_input becomeFirstResponder];
    self.layer.borderColor = [UIColor yellowColor].CGColor;
    self.layer.borderWidth = 1.5;
    self.layer.cornerRadius = 8;
    _input.delegate = self;
    [_input becomeFirstResponder];
    _infoLabel.text = [NSString stringWithFormat:@"Contact the club to get the password\n%@",_number];
    
    //监听键盘
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillShow:)
//                                                 name:UIKeyboardWillShowNotification
//                                               object:nil];
//
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillHide:)
//                                                 name:UIKeyboardWillHideNotification
//                                               object:nil];
}

//当键盘出现
- (void)keyboardWillShow:(NSNotification *)notification{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    int height = keyboardRect.size.height;
    CGFloat time = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect frame = self.frame;
    frame.origin.y = kYFScreenHeight - frame.size.height - height;
    [UIView animateWithDuration:time animations:^{
        self.frame = frame;
    }];
}

//当键退出
- (void)keyboardWillHide:(NSNotification *)notification{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    CGFloat time = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect frame = self.frame;
    frame.origin.y = 0.5*(kYFScreenHeight - frame.size.height);
    [UIView animateWithDuration:time animations:^{
        self.frame = frame;
    }];
}

- (IBAction)confirmAction:(UIButton *)sender {
    [_input resignFirstResponder];
    if (self.dismissHandle) {
        self.dismissHandle(_password);
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@""]) {
        if (_password.length > 0) {
            _password = [_password substringWithRange:NSMakeRange(0, _password.length - 1)];
        }
    }else{
        if (_password == nil) {
            _password = string;
        }else if(_password.length < 5){
            _password = [_password stringByAppendingString:string];
        }
    }
    [self showPassword];
    return YES;
}

- (void)showPassword{
    NSString *showString;
    NSMutableArray *passBuffer = @[@"-",@"-",@"-",@"-",@"-"].mutableCopy;
    for (NSInteger i = 0; i < _password.length; i++) {
        [passBuffer replaceObjectAtIndex:i withObject:[_password substringWithRange:NSMakeRange(i, 1)]];
    }
    for (NSInteger i = 0; i < passBuffer.count; i++) {
        if (showString) {
            showString = [NSString stringWithFormat:@"%@ %@",showString,passBuffer[i]];
        }else{
            showString = passBuffer[i];
        }
    }
    _showLabel.text = showString;
}

@end
